import {createRouter, createWebHashHistory} from 'vue-router'
import type {RouteLocationNormalized} from 'vue-router';
import home from '../views/home.vue'
import storage from '@/utils/storage'

const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: '/',
            name: 'home',
            component: home,
            children: [{
                path: '',
                name: 'HomeView',
                component: () => import('../views/HomeView.vue')
            },{
                path: 'demo',
                name: 'demo',
                component: () => import('../views/demo.vue')
            }]
        },
        {
            path: '/login',
            name: 'login',
            component: () => import('../views/login.vue')
        },
    ]
});

router.beforeEach((to: RouteLocationNormalized) => {
    const token = storage.getItem('token');
    // const permissions = storage.getObject('permissions', []);
    // if (to.meta.permission) {
    //     if ((
    //         (permissions as string[]).includes('ALL') ||
    //         (permissions as string[]).includes((to.meta.permission as string))
    //     )) {
    //
    //     } else {
    //         return {
    //             path: '/accessDenied'
    //         }
    //     }
    // }
    if (to.meta.requiresAuth && !token) {
        return {
            path: '/login',
            query: {redirect: to.fullPath},
        }
    }
});

export default router
